package com.yuke.cloud.service.pmc.service.impl;

import com.yuke.cloud.service.pmc.dao.ProdAttributeMapper;
import com.yuke.cloud.service.pmc.dto.ProdAttributeDto;
import com.yuke.cloud.service.pmc.dto.ProdCategoryAttributeDto;
import com.yuke.cloud.service.pmc.entity.ProdAttribute;
import com.yuke.cloud.service.pmc.entity.ProdCategoryAttribute;
import com.yuke.cloud.service.pmc.entity.ProdSpecAttribute;
import com.yuke.cloud.service.pmc.service.ProdAttributeService;
import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.pmc.service.ProdCategoryAttributeService;
import com.yuke.cloud.service.pmc.service.ProdCategoryService;
import com.yuke.cloud.service.pmc.service.ProdSpecAttributeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by zys on 2018/09/30.
 */
@Slf4j
@Service
@Transactional
public class ProdAttributeServiceImpl extends BaseService<ProdAttribute> implements ProdAttributeService {
    @Resource
    private ProdAttributeMapper prodAttributeMapper;
    @Resource
    private ProdCategoryAttributeService prodCategoryAttributeService;
    @Resource
    private ProdSpecAttributeService prodSpecAttributeService;
    @Resource
    private ProdCategoryService prodCategoryService;

    @Override
    public int save(ProdAttribute record) {
        record.setAttributeId(generateId());
        if (record.getInputType() == (byte)1) {
            record.setInputValues("");
        }
        return super.save(record);
    }

    @Override
    public int update(ProdAttribute entity) {
        if (entity.getInputType() != null && entity.getInputType() == (byte)1) {
            entity.setInputValues("");
        }
        if (entity.getIsEnable() != null && !"".equals(entity.getIsEnable())) {
            //属性禁用启用，商品规格里的属性也禁用启用
            ProdSpecAttribute prodSpecAttribute = new ProdSpecAttribute();
            prodSpecAttribute.setAttributeId(entity.getAttributeId());
            prodSpecAttribute.setIsEnable(entity.getIsEnable());
            Example example = new Example(ProdSpecAttribute.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("attributeId", entity.getAttributeId());
            prodSpecAttributeService.updateByExample(prodSpecAttribute, example);
        }
        return super.update(entity);
    }

    @Override
    public void deleteByIds(String ids) {
        super.deleteByIds(ids);
    }

    @Override
    public void deleteById(Long id) {
        super.deleteById(id);
    }

    @Override
    public Map<String, Integer> checkAttributeId(Long id) {
        log.info("检查AttributeId的外键关联关系");
        Map<String, Integer> map = new HashMap<>();
        Example specAttributeExample = new Example(ProdSpecAttribute.class);
        Example.Criteria specAttributecriteria = specAttributeExample.createCriteria();
        specAttributecriteria.andEqualTo("attributeId", id);
        map.put("prodSpecAttribute", prodSpecAttributeService.selectCountByExample(specAttributeExample));
        Example categoryAttributeExample = new Example(ProdCategoryAttribute.class);
        Example.Criteria categoryAttributecriteria = categoryAttributeExample.createCriteria();
        categoryAttributecriteria.andEqualTo("attributeId", id);
        map.put("prodCategoryAttribute", prodCategoryAttributeService.selectCountByExample(categoryAttributeExample));
        log.info("关联的相关表和数量：" + map.toString());
        return map;
    }

    @Override
    public List<ProdAttributeDto> getListsByCondition(Long categoryId, String attributeName) {
        return prodAttributeMapper.getListsByCondition(categoryId, attributeName);
    }

    /**
     * 获取该分类未绑定的属性
     * @param categoryId
     * @return
     */
    public List<ProdAttributeDto> getAttributeInfoByCategoryId(Long categoryId){
        return prodAttributeMapper.getAttributeInfoByCategoryId(categoryId);
    }
}
